package no.la58.scribbles.server.services;

import no.la58.scribbles.client.services.MessageService;
import no.la58.scribbles.server.storage.ScribbleStorage;
import no.la58.scribbles.shared.GetScribbleRequest;
import no.la58.scribbles.shared.Scribble;
import no.la58.scribbles.shared.ScribbleList;
import no.la58.scribbles.shared.VoidMethodStatus;

import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class MessageServiceImpl extends RemoteServiceServlet implements MessageService {

    ScribbleStorage storage = new ScribbleStorage();

    private static final long serialVersionUID = -8195421462068319046L;
    private com.google.appengine.api.users.UserService googleUserService = UserServiceFactory
	    .getUserService();

    public VoidMethodStatus broadcastScribble(Scribble scribble) {
	if (googleUserService.isUserLoggedIn()) {
	    scribble.setUsermail(googleUserService.getCurrentUser().getNickname());
	    return storage.storeScribble(scribble);
	}
	return VoidMethodStatus.NOT_AUTHENTICATED;
    }

    @Override
    public ScribbleList getScribbles(GetScribbleRequest request) {
	return storage.getScribbles(request);
    }

}
